package data_generator.xml;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import data_generator.entity.SensorNode;

/*Final class to prevent being inherited*/
public final class XMLCreator {
	private XMLCreator() {

	}

	public static void createXML(ArrayList<SensorNode> snList)
			throws IOException {
		Element root = new Element("NodeList").setAttribute("count",
				String.valueOf(snList.size()));
		Document document = new Document(root);
		for (int i = 0; i < snList.size(); i++) {

			Element elements = new Element("Node");

			elements.addContent(new Element("id")
					.setText(snList.get(i).getId()));

			elements.addContent(new Element("name").setText(snList.get(i)
					.getName()));

			Element locationElement = new Element("location");
			locationElement.addContent(new Element("x").setText(String
					.valueOf(snList.get(i).getLocation().getX())));
			locationElement.addContent(new Element("y").setText(String
					.valueOf(snList.get(i).getLocation().getY())));
			locationElement.addContent(new Element("z").setText(String
					.valueOf(snList.get(i).getLocation().getZ())));
			elements.addContent(locationElement);

			Element dataElement = new Element("data");
			int length = snList.get(i).getTemperatureList().size();
			for (int j = 0; j < length; j++) {
				
				Element temperatureElement=new Element("temperature");
				temperatureElement.setAttribute("time", snList
						.get(i).getTemperatureList().get(j).getTime());
				
				dataElement
						.addContent(temperatureElement.setText(String
								.valueOf(snList.get(i).getTemperatureList()
										.get(j).getTemperature())));
			}
			elements.addContent(dataElement);
			
			root.addContent(elements);
		}
  
		Format format=Format.getPrettyFormat();
		XMLOutputter xmlOutputter=new XMLOutputter(format);
		xmlOutputter.output(document, new FileOutputStream("nodelist.xml"));		
	}
}
